package com.v1.admin.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.mongodb.BasicDBObject;
import com.mongodb.CommandResult;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.WriteResult;
import com.v1.lo.Statement;
import com.v1.model.BaseMongoModel;
import com.v1.mongo.Mongos;
import com.v1.util.Constant;

public class LogAction extends AdminAction {

	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private final static String COL = "log";

	public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return mapping.findForward("log");
	}

	public ActionForward search(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		BasicDBObject query = getQuery(request);
		BasicDBObject keys = new BasicDBObject();
		keys.put("_id", 0);
		DBCollection log = getLog();
		CommandResult command = log.getDB().command(new BasicDBObject("collStats", COL));
		long count = log.count(query);
		setAttribute(request, "message", "logs size is " + count + ".<br>collection status is " + command);
		int rowCount = 0;
		if (count > 10000) {
			rowCount = 10000;
		} else {
			rowCount = (int) count;
		}
		Integer pageNo = getInteger(request, "page", 1);
		BaseMongoModel model = new BaseMongoModel();
		Constant constant = (Constant) getBean("Constant");
		Integer logsperpage = constant.getInteger("logs_per_page");
		model.init(rowCount, pageNo, logsperpage);
		setAttribute(request, "pageinfo", model);

		int startIndex = model.getStartIndex();
		int offset = model.getOffset();
		DBCursor cur = log.find(query, keys).sort(new BasicDBObject("timestamp", -1)).skip(startIndex)
				.limit(offset);
		List<DBObject> list = cur.toArray();

		setAttribute(request, "logs", list);
		return mapping.findForward("log");
	}

	private DBCollection getLog() {
		Mongos mongos = (Mongos) getBean("mongos");
		DB db = mongos.getDB(Statement.V1);
		DBCollection log = db.getCollection(COL);
		return log;
	}

	private BasicDBObject getQuery(HttpServletRequest request) throws ParseException {
		String from = getString(request, "from", true);
		BasicDBObject timestamp = new BasicDBObject();
		if (StringUtils.isNotEmpty(from)) {
			timestamp.append("$gte", sdf.parse(from));
		}
		String to = getString(request, "to", true);
		if (StringUtils.isNotEmpty(to)) {
			timestamp.append("$lte", sdf.parse(to));
		}
		BasicDBObject query = new BasicDBObject();
		if (!timestamp.isEmpty()) {
			query.append("timestamp", timestamp);
		}

		String source = getString(request, "source", true);
		if (StringUtils.isNotEmpty(source)) {
			query.put("source", source);
		}
		String level = getString(request, "level", true);
		if (StringUtils.isNotEmpty(level)) {
			query.put("level", level);
		}
		return query;
	}

	public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		BasicDBObject query = getQuery(request);
		DBCollection log = getLog();
		WriteResult result = log.remove(query);
		setAttribute(request, "message", result);
		return search(mapping, form, request, response);
	}
}
